<?php

/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2018-06-23
 * Time: 上午 10:20
 */
class CAd extends CI_Controller
{

    public $user;

    function get_user(){
        $this->user = $this->session->userdata('user_info');
        if(empty($this->user)){
            t_( "<script> location.href = '/login';</script>" );
            exit();
        }
    }

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');
        $this->load->view('weui/jssdk');

    }

    function manage(){
        $this->get_user();
        $this->load->view('cad/base');
        $this->load->view('cad/index');
    }

    function add(){
        $this->get_user();
        $user_id = isset($this->user->id)?$this->user->id:-1;
        if($this->input->get('submit')){
            $data = [];
            $data['pic'] = $this->input->get('pic');
            $data['phone'] = $this->input->get('phone');
            $data['url'] = $this->input->get('url');
            $data['title'] = $this->input->get('title');
            $data['id'] = $this->input->get('id');
            $data['position'] = $this->input->get('position');
            $data['user_id'] = $user_id;
            $this->load->model('ad');
            $this->ad->save($data);
            $this->load->view('cad/base');
            $this->load->view('cad/success');
            return;
        }

        $this->load->view('cad/base');
        $this->load->view('cad/add');
    }


    function ad_list(){
        $this->get_user();
        $user_id = isset($this->user->id)?$this->user->id:-1;
        $this->load->model('ad');
        $ads  = $this->ad->by_user_id($user_id);
        $this->load->vars("cad",$ads);
        $this->load->view('cad/base');
        $this->load->view('cad/list');
    }
    function ad_delete(){
        $id = $this->input->get('id');
        $this->load->model('ad');
        $this->ad->del_by_id($id);
        $this->load->view('cad/base');
        $this->load->view('cad/success');
    }

}